<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class BaseController extends Controller {
	var $_template = "web";
	var $_layout = "layout";
	var $_templatePath = "templates/";
	
	var $_data = array();
	
	var $_directory;
	var $_controller;
	var $_method;
	var $_param;
	
	function BaseController(){
	    parent::Controller();
	    
	    $this->_template = $this->settings->item("_site_template");
		$this->_layout = $this->settings->item("_site_layout");
	    
	    $this->_data = array(
	        'slots'   => array(),
	        'scripts' => array(),
	        'styles'  => array(),
	        'meta' => array()
	    );
	    
	    $this->config->set_item("template_name", $this->_template);	
	    
	    $this->_directory = $this->router->directory;
	    $this->_controller = $this->router->class;
		$this->_method = $this->router->method;
       
		if($this->redux_auth->logged_in()){       	
			$this->_data['login_user'] = $this->redux_auth->profile();
			$this->template->addData('login_user', $this->redux_auth->profile());
			//Solo la consola de debug si estamos logado sy somos administradores
			
			$profile = $this->redux_auth->profile();
			
			$enable_profiler = $this->settings->item("_site_enable_profiler");
			if($enable_profiler !== NULL && $enable_profiler && $profile->group == "admin"){
				$this->output->enable_profiler ( TRUE );
			}
		}
		
		//Almacenamos el referrer
		$referrer = $this->input->get_post("referrer");
		
		if($referrer == NULL){
			$referrer = $this->session->flashdata("referrer");
		}

		if ($referrer == NULL){
			$this->load->library('user_agent');
	    	$referrer = $this->agent->referrer();
		}
		
		$this->session->set_userdata("referrer", $referrer);
		$this->_data['referrer'] = $referrer;
		$this->template->addData('referrer', $referrer);
		
		
		//Libreria template
		$this->template->setTheme($this->settings->item("_site_template"));
		$this->template->setLayout($this->settings->item("_site_layout"));
		$this->template->setPage("{$this->router->class}/{$this->router->method}");
        
	}
	
	function _render(){
		if(!isset($this->_data ['page']) || $this->_data ['page'] == NULL){
			$this->_data ['page'] = $this->_controller . "/" . $this->_method;
		}
		
		/*if($this->_directory != NULL && !$this->_directory == ""){
			$this->_data ['page'] = $this->_directory . $this->_data ['page'];
		}*/
		
		$this->config->set_item("template_name", $this->_template);	
		$this->_data['template_path'] = $this->_templatePath . $this->_template . "/";
		
		
		if(isset($_GET) && $_GET != null && isset($_GET['viewdata'])){
			echo "<pre>";
			print_r($this->_data);
			echo "</pre>";
		}else{
			$this->load->view ( $this->_templatePath . $this->_template . "/layouts/" . $this->_layout, $this->_data );
		}
	}
	
	function _setTemplate($name){
		$this->_template = $name;			
	}
	
	function _setLayout($name){
		$this->_layout = $name;
	}
	
	function _setPageView($page){
		$this->_data ['page'] = $page;
	}
	
	// add a component to be included
	function _addSlot($name, $path, $data=null) {
		//Relativo al path? $path = $this->_templatePath . $path
		
		$this->_data['slots'][$name]=&$this->load->view($this->_templatePath . $this->_template . $path, $data, true);
	    //$this->_data['slots'][$name]=&$this->load->view($path, $data, true);
	    
	}
	
	// add a HTML meta tag
	function _addMeta($name, $content) {
	    $this->_data['meta'][$name]=$content;
	}
	
	// add an external javascript
	function _addScript($path) {
	    $this->_data['scripts'][]=$path;
	}
	
	// add an external stylesheet
	function _addStyle($path) {
	    $this->_data['styles'][]=$path;
	}
	
	// add a simple template variable
	function _addData($name, $value, $add = FALSE) {
		if($add){
			$data = NULL;
			if(!isset($this->_data[$name]) || $this->_data[$name] == NULL){
				$data = array();
			} else{
				$data = $this->_data[$name];
			}
			array_push($data, $value);
			
			$this->_data[$name] = $data;
		} else{
	    	$this->_data[$name]=$value;
		}
	}
	
	// add a big template variable by reference
	function _addDataByRef($name, &$value) {
	    $this->_data[$name]=$value;
	}
	
	
	function _addInformation($text){
		$this->_addData("information", $text, TRUE);
	}
	
	function _addAttention($text){
		$this->_addData("attention", $text, TRUE);
	}
	
	function _addError($text){
		$this->_addData("error", $text, TRUE);
	}
	
	function _addSuccess($text){
		$this->_addData("success", $text, TRUE);
	}
	
	/** Add Flash **/
	function _addFlashInformation($text){
		$this->_addFlashData("information", $text);
	}
	
	function _addFlashAttention($text){
		$this->_addFlashData("attention", $text);
	}
	
	function _addFlashError($text){
		$this->_addFlashData("error", $text);
	}
	
	function _addFlashSuccess($text){
		$this->_addFlashData("success", $text);
	}
	
	function _addFlashData($tipo, $message){
		$data = $this->session->flashdata($tipo);
		if(!isset($data) || $data == NULL){
			$data = array();
		}
		array_push($data, $message);
		
		$this->session->set_flashdata($tipo, $data);
	}
	
	function _sendMail($subject, $to, $data, $mailViewPath){
		$fromMail = "smorete@gmail.com";
		$fromName = "S&S";
		
		$this->load->library('email');
		$this->email->set_newline("\r\n");
		
		$this->email->from($fromMail, $fromName);
		$this->email->to($to);

		$this->email->subject($subject);
		
		$msg = $this->load->view($this->_templatePath . $mailViewPath, $data, TRUE);		
		$this->email->message($msg);

		return $this->email->send();
	}
}
